<template>
    <div>
        <mt-search @keyup.enter.native="searchLocel" v-model="search" placeholder="搜索" :show="showResult">
            <mt-cell v-for="item in result" :title="item.title" :to="item.link" :is-link="item.islink">
                {{item.value}}&nbsp;&nbsp;<mt-button v-show="item.showbotton" @click.native="addTask(item.url)" size="small" type="primary">添加</mt-button>
            </mt-cell>
            <mt-button @click.native="searchRemote" style="margin-top:25px;" v-show="showSearchRemote" size="large">load站外小说 (～ o ～)~zZ...</mt-button>
        </mt-search>
    </div>
</template>

<style lang="css">

</style>

<script type="text/babel">
    import { MessageBox } from 'mint-ui'
    import { Indicator } from 'mint-ui'

    export default{
        data(){
            return{
                search:"",
                result:[],
                showResult:true,
                showSearchRemote:false,
            }
        },
        methods: {
            searchLocel: function() {
                Indicator.open({
                    text: '加载中...',
                    spinnerType: 'fading-circle'
                })
                let _this = this
                _this.result = []
                _this.$http.post("https://novel.nightc.com/v1/search/local", {
                    title:_this.search
                }).then((resp) => {
                    let body = resp.body
                    if (body != undefined && body.code == 0) {
                        for (let i=0;i<body.data.length;i++) {
                            let search_data = {}
                            search_data.title = body.data[i].title
                            search_data.value = body.data[i].auth
                            search_data.link = "/novel/" + body.data[i].id
                            search_data.islink = true
                            search_data.url = ""
                            search_data.showbotton = false
                            _this.result.push(search_data)
                        }
                        Indicator.close()
                        _this.showSearchRemote = true
                        _this.showResult = true
                    }
                },(resp) => {
                    Indicator.close()
                    let body = resp.body
                    if (body != undefined) {
                        MessageBox('提示', body.data);
                    }
                })
            },
            searchRemote: function() {
                Indicator.open({
                    text: '加载中...',
                    spinnerType: 'fading-circle'
                })
                let _this = this
                _this.showSearchRemote = false
                _this.$http.post("https://novel.nightc.com/v1/search/remote", {
                    title:_this.search
                }).then((resp) => {
                    let body = resp.body
                    if (body != undefined && body.code == 0) {
                        let data = body.Data
                        if(data.length == 0) {
                            MessageBox('提示', "抱歉找不到此小说");
                        }
                        for (let i=0;i<data.length;i++) {
                            let search_data = {}
                            if (data[i] != undefined && data[i].Novel != undefined) {
                                search_data.title = data[i].Novel.Title
                                search_data.value = data[i].Novel.Auth
                                search_data.link = "search"
                                search_data.url = data[i].Novel.From
                                search_data.islink = false
                                search_data.showbotton = true
                                _this.result.push(search_data)
                            }
                        }
                        Indicator.close()
                        _this.showResult = true
                    }
                },(resp) => {
                    Indicator.close()
                    let body = resp.body
                    if (body != undefined) {
                        MessageBox('提示', body.data);
                    }
                })
            },
            addTask: function(url) {
                let _this = this
                Indicator.open({
                    text: '加载中...',
                    spinnerType: 'fading-circle'
                })
                _this.$http.post("https://novel.nightc.com/v1/novel/task", {
                    url:url
                }).then((resp) => {
                    let body = resp.body
                    Indicator.close()
                    if (body != undefined && body.code == 0) {
                        MessageBox('^_^', '后台添加中^_^');
                    }
                },(resp) => {
                    Indicator.close()
                    let body = resp.body
                    if (body != undefined) {
                        MessageBox('提示', body.data);
                    }
                })
            }
        }
    }
</script>
